package com.ws.common.base.service.Impl;

import com.ws.common.base.dao.BaseMapper;
import com.ws.common.base.entity.Columns;
import com.ws.common.base.service.IColumnsService;
import com.ws.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ColumnsServiceImpl implements IColumnsService{
    @Autowired
    BaseMapper<Columns> baseMapper;

    private List<Columns> columns;

    @Value("${spring.datasource.url}")
    private String datasourceUrl;


    @Override
    public List<Columns> selectColumnsByTableName(String tableName) {
        /*tableName=tableName.toLowerCase();
        if(columns==null){
            columns=baseMapper.columnsList(getMysqlName(),tableName);
        }
        List<Columns> result= new ArrayList<Columns>();
        for (Columns column: columns){
            String t=column.getTableName().toLowerCase();
            if(StrUtil.equals(t,tableName) && StrUtil.equals(column.getTableSchema(),this.getMysqlName())){
                //System.out.println(column.getTableSchema()+" "+t+"---"+column.getColumnName()+"   ");
                result.add(column);
            }
        }*/
        tableName= StringUtils.camelToUnderline(tableName);
        return baseMapper.columnsList(getMysqlName(),tableName);
    }

    @Override
    public String getMysqlName(){
        String myUrl=datasourceUrl.split("\\?")[0];
        String []str=myUrl.split("/");
        return str[str.length-1];
    }
}
